//
//  STTools_SegementTitleView.m
//  catVoice
//
//  Created by apple on 2019/6/16.
//  Copyright © 2019 imac. All rights reserved.
//

#import "STTools_SegementTitleView.h"
@interface STTools_SegementTitleView()
@property(nonatomic, strong) UIScrollView                     *manuScrollView;/**<  */
@property(nonatomic, strong) NSMutableArray                     *menuButtoArray;/**<  */
@property(nonatomic, strong) NSArray                     *titles;/**<  */
@end
@implementation STTools_SegementTitleView
- (instancetype)initWithFrame:(CGRect)frame titles:(NSArray*)titles
{
    self = [super initWithFrame:frame];
    if (self) {
        self.titles = titles;
        [self stoneobs_ui_configSubView];
    }
    return self;
}
- (void)setButtonBgColor:(UIColor *)buttonBgColor{
    _buttonBgColor = buttonBgColor;
    [self stoneobs_ui_configSubView];
}
- (void)setButtonSelctedBgColor:(UIColor *)buttonSelctedBgColor{
    _buttonSelctedBgColor = buttonSelctedBgColor;
    [self stoneobs_ui_configSubView];
}
- (void)setButtonTitleColor:(UIColor *)buttonTitleColor{
    _buttonTitleColor = buttonTitleColor;
    [self stoneobs_ui_configSubView];
}
- (void)setButtonSelctedTitleColor:(UIColor *)buttonSelctedTitleColor{
    _buttonSelctedTitleColor = buttonSelctedTitleColor;
    [self stoneobs_ui_configSubView];
}
- (void)setForceWitdh:(CGFloat)forceWitdh{
    _forceWitdh = forceWitdh;
    [self stoneobs_ui_configSubView];
}
#pragma mark --stoneobs_ui_configSubView
- (void)stoneobs_ui_configSubView{
    [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    UIScrollView * buttFaterView = [[UIScrollView alloc] initWithFrame:self.bounds];
    buttFaterView.left = 0;
    buttFaterView.layer.cornerRadius = 4;
    buttFaterView.clipsToBounds = YES;
    buttFaterView.backgroundColor = UIColor.clearColor;
    [self addSubview:buttFaterView];
    self.manuScrollView =  buttFaterView;
    if (@available(iOS 11.0, *)) {
        buttFaterView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        // Fallback on earlier versions
    }
    self.menuButtoArray = NSMutableArray.new;
    CGFloat left = 0;
    NSArray * array = self.titles;
    __weak typeof(self) weakSelf =  self;
    for (NSInteger i = 0; i < array.count; i ++) {
        NSString * form_name = array[i];
        CGFloat form_nameWitdh = [form_name stoneobs_fun_st_widthWithheight:32 font:14];
        CGFloat adjustWidth = form_nameWitdh + self.height + 10;
        if (self.forceWitdh) {
            adjustWidth = self.forceWitdh;
        }
        STButton * button = [[STButton alloc] initWithFrame:CGRectMake(left, 0, adjustWidth, self.height)
                                                      title:form_name
                                                 titleColor:UIColor.whiteColor
                                                  titleFont:14
                                               cornerRadius:self.height/2
                                            backgroundColor:UIColor.whiteColor
                                            backgroundImage:nil
                                                      image:nil];
        [button stoneobs_fun_touch_handle:^(UIButton *sender) {
            [weakSelf stoneobs_action_onSlectedButton:sender];
        }];
        //        [button setTitleColor:UIColor.whiteColor forState:UIControlStateSelected];
        button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        [buttFaterView addSubview:button];
        [self.menuButtoArray addObject:button];
        //        [button stoneobs_fun_st_setBorderWith:0.5 borderColor:STONEOBS_UI_THEME_BACKGROUNDCOLOR cornerRadius:4];
        left = button.right - button.layer.cornerRadius;
        buttFaterView.width = button.right;
        button.tag = 10000 + i;
        self.width = buttFaterView.right;
        if (buttFaterView.width > self.width) {
            buttFaterView.width = self.width;
            buttFaterView.contentSize = CGSizeMake(left, 0);
        }

        if (self.buttonBgColor) {
            button.backgroundColor = self.buttonBgColor;
        }
        if (self.buttonTitleColor) {
            [button setTitleColor:self.buttonTitleColor forState:UIControlStateNormal];
        }
        if (self.buttonSelctedTitleColor) {
            [button setTitleColor:self.buttonSelctedTitleColor forState:UIControlStateSelected];
        }
    }
    
    [buttFaterView stoneobs_fun_st_setBorderWith:0.5 borderColor:STONEOBS_UI_THEME_BACKGROUNDCOLOR cornerRadius:16];
    [self stoneobs_action_onSlectedButton:self.menuButtoArray.firstObject];
}
- (void)stoneobs_action_onSlectedButton:(UIButton*)sender{
    [self.manuScrollView bringSubviewToFront:sender];
    
    for (UIButton * buton in self.menuButtoArray) {
        buton.backgroundColor = UIColor.clearColor;;
        if (self.buttonBgColor) {
            buton.backgroundColor = self.buttonBgColor;
        }
        buton.selected = NO;
    }
    sender.selected = YES;

    sender.backgroundColor = STONEOBS_UI_THEME_BACKGROUNDCOLOR;
    if (self.buttonSelctedBgColor) {
        sender.backgroundColor = self.buttonSelctedBgColor;
    }
    NSInteger index = (sender.tag- 10000);
    _currentIndex = index;
    if (self.didSelctedButton) {
        self.didSelctedButton(self, sender);
    }
}
- (void)setCurrentIndex:(NSInteger)currentIndex{
    _currentIndex = currentIndex;
    UIButton * sender = self.menuButtoArray[currentIndex];
    [self.manuScrollView bringSubviewToFront:sender];
    
    for (UIButton * buton in self.menuButtoArray) {
        buton.backgroundColor = UIColor.clearColor;;
        [buton setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
    }
    [sender setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
    sender.backgroundColor = STONEOBS_UI_THEME_BACKGROUNDCOLOR;
}
@end
